<?php

namespace App\Http\Controllers\Cruise\Web;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Cruise\CruiseCheckSelfTaskValidate;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckSelfTaskService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CruiseCheckSelfTaskController extends BaseController
{
    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function index(Request $request): JsonResponse
    {
        $name                 = $request->get("name", "");
        $cruiseCheckCompanyId = (int)$request->get("cruiseCheckCompanyId", 0);
        $pageSize             = $request->get('pageSize', 10);
        $pageNum              = $request->get('pageNum', 1);
        $templateType         = $request->get('templateType', CruiseTemplateModel::TYPE_5);
        $shopIds              = $request->get('shopIds', '');

        if ($shopIds) {
            $shopIds = explode(',', $shopIds);
        } else {
            $shopIds = [];
        }

        return $this->success(CruiseCheckSelfTaskService::index($pageNum, $pageSize, $name, $cruiseCheckCompanyId, $templateType, $shopIds));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function insert(Request $request): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::insertValidate($request->all());
        CruiseCheckSelfTaskService::insert($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function update(Request $request): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::updateValidate($request->all());
        CruiseCheckSelfTaskService::update($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function status(Request $request): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::statusValidate($request->all());
        CruiseCheckSelfTaskService::status($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function detail(Request $request): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::detailValidate($request->all());
        return $this->success(CruiseCheckSelfTaskService::detail($params));
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function del(Request $request): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::detailValidate($request->all());
        CruiseCheckSelfTaskService::del($params);
        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function finishCondition(Request $request): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::detailValidate($request->all());
        return $this->success(CruiseCheckSelfTaskService::finishCondition($params));
    }

    /**
     * 工厂自检 - 供应商任务聚合统计
     *
     * @return JsonResponse
     * @throws ValidationException
     * @throws GuzzleException
     */
    public function supplierTaskGroupPreview(): JsonResponse
    {
        return $this->success(CruiseCheckSelfTaskService::supplierTaskGroupPreview());
    }

    /**
     * 工厂自检 - 供应商任务列表
     *
     * @param Request $request
     * @param int $cruiseCheckCompanyId
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function supplierTaskPreview(Request $request, int $cruiseCheckCompanyId): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::supplierTaskPreviewValidate(
            $request->merge(["cruiseCheckCompanyId" => $cruiseCheckCompanyId])->all()
        );
        return $this->success(CruiseCheckSelfTaskService::supplierTaskPreview($params));
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function todayNotProduce(Request $request): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::todayNotProduceValidate($request->all());
        CruiseCheckSelfTaskService::todayNotProduce($params);
        return $this->success();
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function supplierTaskDetail(Request $request): JsonResponse
    {
        $params = CruiseCheckSelfTaskValidate::supplierTaskDetailValidate($request->all());
        return $this->success(CruiseCheckSelfTaskService::supplierTaskDetail($params));
    }
}
